package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.UserCollection;
import com.codeyang.es.ProdEs;
import com.codeyang.service.UserCollectionService;
import com.codeyang.util.LoadUserInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/28 10:00
 */
@Api("用户收藏接口管理")
@RestController
@RequestMapping("p/collection")
public class UserConnectionController {

    @Autowired
    private UserCollectionService userCollectionService;

    @GetMapping("count")
    @ApiOperation("用户收藏数量查询")
    public ResponseEntity<Integer> loadCount() {
        String userId = LoadUserInfoUtil.getUserId();
        int count = userCollectionService.count(
                new LambdaQueryWrapper<UserCollection>()
                        .eq(UserCollection::getUserId, userId));
        return ResponseEntity.ok(count);
    }







    @GetMapping("isCollection")
    @ApiOperation("判断用户是否收藏")
    public ResponseEntity<Boolean> isCollection(@RequestParam Long prodId) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getProdId, prodId)
        );
        return ResponseEntity.ok(count > 0);
    }


    @PostMapping("addOrCancel")
    @ApiOperation("用户添加或者取消收藏")
    public ResponseEntity<Void> addOrCancel(@RequestBody Long prodId) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        userCollectionService.addOrCancel(userId, prodId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("prods")
    @ApiOperation("加载用户收藏商品接口")
    public ResponseEntity<Page<ProdEs>> loadUserCollection(Page<UserCollection> page) {
        String userId = LoadUserInfoUtil.getUserId();
        Page<ProdEs> prodEsPage = userCollectionService.findUserCollectPage(page, userId);
        return ResponseEntity.ok(prodEsPage);
    }


}
